import { Props } from '@/components/typing';
import { isObject } from '@/utils/type-validate';
import { Prop } from 'vue';
import moment from 'moment'

export const cloneForm = (source: Props, target: Props, field: string = 'id') => {
  if(!source) source = {};
  if(target) {
    Object.keys(target).forEach(key => {
      if(Array.isArray(target[key])) {
        source[key] = target[key].map((item: any) => item[field]);
      }else if (target[key] instanceof moment){
        source[key] = target[key];
      }else if(isObject(target[key])) {
        source[key] = target[key][field];
      } else {
        source[key] = target[key];
      }   
    });
  }
}
export const clearForm = (form: Props) => {
  if(form) {
    Object.keys(form).forEach(key => delete form[key]);
  }
}

export const shallowClone = (source: object) => {
  return Object.assign({} as object,source)
}

export const cloneObject = (source: Props)=>{
  let o:Props = {};
  Object.keys(source).forEach(key => o[key] = source[key])
  return o;
}